using System;
using System.Collections.Generic;
using System.Text;
using SlimDX;
using SlimDX.Direct3D9;
using Barrage.Global;

namespace Barrage.Textures
{
    /// <summary>
    /// Implements a singleton cube texture cache with autoload functions
    /// </summary>
    public class CubeTextureManager : IDisposable
    {
        private static CubeTextureManager instance=null;

        /// <summary>
        /// Returns the class instance.
        /// </summary>
        public static CubeTextureManager Textures { get { if (instance == null) instance = new CubeTextureManager(); return instance; } }

        private CubeTextureManager() { 
            createDefaultTexture();
            Engine.RegisterDisposeHandler(new EngineDisposeHandler(Dispose));
        }
        private Dictionary<string, CubeTexture> textures = new Dictionary<string, CubeTexture>();

        private CubeTexture defaultTexture;
        /// <summary>
        /// Returns the default texture (default_cube.dds) that's loaded when loaded textures return error
        /// </summary>
        public static CubeTexture DefaultTexture { get { return instance.defaultTexture; } }

        /// <summary>
        /// Creates the default texture (default_cube.dds) that's loaded when loaded textures return error
        /// </summary>
        protected void createDefaultTexture()
        {
            defaultTexture = CubeTexture.FromFile(Engine.Device, Store.PathConfig[Store.TEXTURE_PATH]+"default_cube.dds");
        }

        /// <summary>
        /// Clear resources
        /// </summary>
        public void Dispose()
        {
            foreach (CubeTexture t in textures.Values)
                t.Dispose();
            defaultTexture.Dispose();
            defaultTexture = null;
            textures.Clear();
            instance = null;
            NLog.LogManager.GetLogger(ToString()).Debug("Disposing");
            GC.SuppressFinalize(this);
        }


        /// <summary>
        /// Tries to load the cube texture with the specified name (in the TexturePath folder)
        /// </summary>
        /// <param name="name">texture name</param>
        /// <returns>If everything went well, the loaded cube texture. Else, the default cube texture</returns>
        public CubeTexture this[string name]
        {
            get
            {
                CubeTexture t = null;
                if (textures.TryGetValue(name,out t))
                    return t;
                NLog.LogManager.GetLogger(ToString()).Warn("Texture " + name + " not found. Trying to load");
                try
                {
                    t = CubeTexture.FromFile(Barrage.Global.Engine.Device, Store.PathConfig[Store.TEXTURE_PATH] + name, Usage.None, Pool.Managed);
                    NLog.LogManager.GetLogger(ToString()).Debug("Texture " + name + " finished loading.");
                }
                catch{
                    NLog.LogManager.GetLogger(ToString()).Error("Error loading texture " + name);
                    t = defaultTexture;
                }
                textures.Add(name, t);
                return t;
            }
        }

        /// <summary>
        /// Destructor. Clears resources
        /// </summary>
        ~CubeTextureManager()
        {
            Dispose();
        }
    }
}
